package algorithm.ldk.SelectionSort;

/**
 * @作者：ldk
 * @email 2505496685@qq.com
 * @描述：默认描述
 * @创建时间：16:55 2021-01-03
 * @修改时间：16:55 2021-01-03
 * @修改描述：默认描述
 */
public class SortingHelper {

    private SortingHelper(){}

    public static <E extends Comparable<E>> boolean isSorted(E[] arr){

        for (int i = 1; i < arr.length; i ++){
            if(arr[i-1].compareTo(arr[i]) > 0){
                return false;
            }
        }
        return true;
    }

    public static <E extends Comparable<E>> void sortTest(String sortName, E[] arr){

        long startTime = System.nanoTime();
        if(sortName.equals("SelectionSort")){
            SelectionSort.sort(arr);
        }else if(sortName.equals("InsertionSort")){
            InsertionSort.sort(arr);
        }


        long endTime = System.nanoTime();

        double time = (endTime - startTime) / 1000000000.0;

        if(!SortingHelper.isSorted(arr)){
            throw new RuntimeException(sortName + " failed");
        }
        System.out.println(String.format("%s , n = %d : %f s",sortName, arr.length, time));
    }
}
